using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using Entity;
using Utilities;

namespace DAL
{
	#region Generated code
    public class BlueProductDataAccess : FreshCmsAccessBase
    {
        public List<BlueProduct> GetBlueProductList()
        {
            try
            {
                const string spName = "sp_BlueProduct_GetList";

                var blueProductList = DbHelper.ExecuteSPList<BlueProduct>(spName);
                return blueProductList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return new List<BlueProduct>();
            }
        }

        public List<BlueProduct> GetBlueProductListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        {
            try
            {
                const string spName = "sp_BlueProduct_GetByPaging";
                var sqlParams = new[]
                                    {
										new SqlParameter("@PageIndex", pageIndex),
										new SqlParameter("@PageSize", pageSize),
										new SqlParameter("@TotalRecord", totalRecord)
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
                var blueProductList = DbHelper.ExecuteSqlList<BlueProduct>(sqlCommand);
				if (sqlCommand.Parameters["@TotalRecord"].Value != null)
                {
                    totalRecord = (int) sqlCommand.Parameters["@TotalRecord"].Value;
                }

                return blueProductList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return new List<BlueProduct>();
            }
        }

        public BlueProduct GetBlueProductById(int proId)
        {
            try
            {
                const string spName = "sp_BlueProduct_GetById";
                var sqlParams = new[]
                                    {
										new SqlParameter("@ProId", proId)
                                    };

                var blueProduct = DbHelper.ExecuteSPList<BlueProduct>(spName, sqlParams).SingleOrDefault();
                return blueProduct;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return null;
            }
        }

        public int InsertBlueProduct(BlueProduct blueProduct)
        {
            try
            {
                const string spName = "sp_BlueProduct_Insert";
                var sqlParams = new[]
                                    {

										new SqlParameter("@ProName", blueProduct.ProName),
										new SqlParameter("@ProPrice", blueProduct.ProPrice),
										new SqlParameter("@ProDesc", blueProduct.ProDesc),
										new SqlParameter("@ProTotalQuantity", blueProduct.ProTotalQuantity),
										new SqlParameter("@ProNote", blueProduct.ProNote),
										new SqlParameter("@ProId", blueProduct.ProId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@ProId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@ProId"].Value != null)
                {
                    return (int) sqlCommand.Parameters["@ProId"].Value;
                }

				return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return -1;
            }
        }

        public int UpdateBlueProduct(BlueProduct blueProduct)
        {
            try
            {
                const string spName = "sp_BlueProduct_Update";
                var sqlParams = new[]
                                    {
										new SqlParameter("@ProId", blueProduct.ProId)
										, new SqlParameter("@ProName", blueProduct.ProName)
										, new SqlParameter("@ProPrice", blueProduct.ProPrice)
										, new SqlParameter("@ProDesc", blueProduct.ProDesc)
										, new SqlParameter("@ProTotalQuantity", blueProduct.ProTotalQuantity)
										, new SqlParameter("@ProNote", blueProduct.ProNote)
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return -1;
            }
        }

        public int DeleteBlueProduct(int proId)
        {
            try
            {
                const string spName = "sp_BlueProduct_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@ProId", proId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());

                return -1;
            }
        }
		#endregion
    }
}
